//
//  AY_BookVpCell.m
//  AiYue
//
//  Created by boWen on 2023/10/26.
//

#import "AY_BookVpCell.h"
#import "AY_BookVpItemCell.h"

@interface AY_BookVpCell ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic,strong) UIView * bg_View;//底部圆角
@property (nonatomic,strong) UILabel * selectLabel;
@property (nonatomic, strong) UICollectionView * myCollectionView;
@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation AY_BookVpCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addAllSubviewsWithUI];
    }
    return self;
}


- (void)addAllSubviewsWithUI {
    self.currentIndex = 0;
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.contentView.backgroundColor = COLOR(@"#19171A");
    
    self.bg_View = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(13) andAddSubView:self.contentView];
    [self.bg_View mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.contentView);
        make.bottom.equalTo(self.contentView).offset(YNUM(10));
    }];

    self.selectLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"选择套餐" andTextColor:COLOR(@"#010101") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self.bg_View];
    [self.selectLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bg_View).offset(YNUM(15));
        make.left.equalTo(self.bg_View).offset(YNUM(15));
        make.right.equalTo(self.bg_View).offset(YNUM(-15));
        make.height.mas_offset(YNUM(24));
    }];
    
    
    [self.bg_View addSubview:self.myCollectionView];
    [self.myCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bg_View);
        make.top.equalTo(self.selectLabel.mas_bottom);
        make.left.equalTo(self.bg_View).offset(YNUM(12));
        make.right.equalTo(self.bg_View).offset(YNUM(-12));
    }];
}

- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.itemSize = CGSizeMake(YNUM(94), YNUM(124));
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        layout.minimumLineSpacing = YNUM(8);
        layout.minimumInteritemSpacing = 0;
        UICollectionView * collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        collectionView.dataSource = self;
        collectionView.delegate = self;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.backgroundColor = [UIColor whiteColor];
        [collectionView registerClass:[AY_BookVpItemCell class] forCellWithReuseIdentifier:@"AY_BookVpItemCell"];
        _myCollectionView = collectionView;
    }
    return _myCollectionView;
}

#pragma mark -- UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.vpDataAry.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    AY_BookVpItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AY_BookVpItemCell" forIndexPath:indexPath];
    VpProductModel *model = self.vpDataAry[indexPath.row];
    if (indexPath.row == self.currentIndex) {
        model.isSel = @"1";
    }else {
        model.isSel = @"0";
    }
    cell.vpSubModel = model;
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    self.currentIndex = indexPath.row;
    if(self.vpUpdateBlock) {
        self.vpUpdateBlock(indexPath.row);
    }
    [self.myCollectionView reloadData];
}


- (void)setVpDataAry:(NSArray *)vpDataAry {
    _vpDataAry = vpDataAry;
    if(_vpDataAry.count > 0) {
        [self.myCollectionView reloadData];
        [self.myCollectionView selectItemAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UICollectionViewScrollPositionCenteredHorizontally];
    }
}

@end
